package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private CommentService commentService;


    /**
     * 发布动态
     *
     * @param movement
     * @param imageContent
     * @return
     * @throws IOException
     */
    @PostMapping
    public ResponseEntity movements(Movement movement,
                                    MultipartFile[] imageContent) throws IOException {
        movementsService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询个人状态
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity all(Long userId,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementsService.all(userId, page, pagesize);

        return ResponseEntity.ok(pr);

    }

    /**
     * 好友动态查询
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementsService.movements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 推荐动态查询
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementsService.recommend(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 通过id查询动态
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo vo = movementsService.findById(movementId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 动态点赞
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer count = commentService.like(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 动态取消点赞
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer Count = commentService.dislike(movementId);
        return ResponseEntity.ok(Count);
    }

    /**
     * 动态喜欢
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer count = commentService.love(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 动态取消喜欢
     *
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislove")
    public ResponseEntity dislove(@PathVariable("id") String movementId) {
        Integer Count = commentService.dislove(movementId);
        return ResponseEntity.ok(Count);
    }

    /**
     * 谁看过我
     */
    @GetMapping("visitors")
    public ResponseEntity queryVisitorsList(){
        List<VisitorsVo> list = movementsService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }
}
